package controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Books;
import pojo.Healths;
import pojo.Users;
import service.BookService;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/allUserPeople",produces="text/html;charset=utf-8")
    public String allHealth(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control","no-cache");
        List<Users> list = userService.queryAllUser();
        String resJSON = JSON.toJSONString(list);     // 转换为json
        return resJSON;
    }

    @RequestMapping("/allUser")
    public String list(Model model){
        List<Users> list = userService.queryAllUser();
        model.addAttribute("list",list);
        System.out.println(list);
        return "usersManager/usersManager";
    }

    @RequestMapping("/toAddUser")
    public String toAddUser(){
        return "usersManager/AddUser";
    }
//    增加
    @RequestMapping("/addUser")
    public String addUser(Users users){
        System.out.println("增加"+users);
        userService.addUser(users);
        return "redirect:/allUser";
    }

    @RequestMapping("/toUpdateUser")
    public String toUpdateUser(Model model,int id){
        Users users = userService.queryUserById(id);
        System.out.println(users);
        model.addAttribute("users",users);
        return "usersManager/updateUser";
    }

    @RequestMapping("/updateUser")
    public String updateUser(Model model,Users user){
        System.out.println("更新"+user);
        userService.updateUser(user);
        Users users = userService.queryUserById(user.getId());
        model.addAttribute("users", users);
        return "redirect:/allUser";
    }

    //    删除
    @RequestMapping("/delUser/{id}")
    public String deleteUser(@PathVariable("id") int id){
        System.out.println(id);
        userService.deleteUserById(id);
        return "redirect:/allUser";
    }

    //    删除
    @RequestMapping("/delUserBy/{id}")
    public void deleteBook(@PathVariable("id") int id){
        System.out.println(id);
        userService.deleteUserById(id);
    }
//    查询书籍
    @RequestMapping("/queryUser")
    public String queryUserByName(String queryUserName,Model model){
        List<Users> list = userService.queryUser(queryUserName);
        System.out.println(list);
        model.addAttribute("list",list);
        return "allUser";
    }
}
